<?php
include "vendor/autoload.php";

use GatewayClient\Gateway;

Gateway::$registerAddress = '127.0.0.1:1238';



if($_GET || $_POST){

	$post = file_get_contents("php://input");
	$psot = str_replace('\\',"",$post);
        file_put_contents('./tmp/log'.time(),$post);
        $array = json_decode($post,true);
        $qr_id =json_decode(urldecode($array['msg']),true)['qr_info']['qr_id'];
		$status = json_decode($post,true)['status'];
		switch ($status) {
			case 'WAIT_BUYER_PAY':
			$status = 2;
			break;
			case 'TRADE_PAID':
				$status = 1;
				break;
			case 'TRADE_SUCCESS':
				$status = 1;
				break;
			default:
				$status = 0;
				break;
		}
        
        $qr_id?file_put_contents('./tmp/'.$qr_id,$post):null;
        
        if($status){
        		try {

					$pdo = new PDO('mysql:host=localhost;dbname=shop','root','19960521');

					//echo "insert into pay (client_id,kdt_id) values ($client_id,$kdt_id)";

					$pm = $pdo -> query("select client_id from pay where kdt_id = $qr_id");

					$client_id = $pm->fetch(PDO::FETCH_ASSOC)['client_id'];
					$data = json_encode(['status'=>$status]);
					Gateway::sendToClient($client_id, $data);
					//
					
					} catch (PDOException $e) {

				    die ("Error!: " . $e->getMessage() . "<br/>");
					}

        }
        echo '{"code":0,"msg":"success"}';

}








//mysql connect

// function mysql($kdt_id){
// 	try {

// 	$pdo = new PDO('mysql:host=localhost;dbname=pay');

// 	$pm = $pdo -> query("select * from pay where id = $kdt_id");

// 	} catch (PDOException $e) {

//     die ("Error!: " . $e->getMessage() . "<br/>");
// 	}
// }
// $mysql($client);
